/**
 * 频率拦截器
 * 用来拦截ddos攻击
 */
global.ipintercept={};
global.interceptlibrary=[];
if(module)module.exports = frequen;
export default function frequen ({req,res}:any):Object|void{
    //设置返回代码
    if(global.interceptlibrary.includes(req.headers?.origin)){res.statusCode=403; return "访问频繁"}
    //有对方的ip
    if(req.headers.origin){
        if(global.ipintercept[req.headers.origin]){
            //存在
            global.ipintercept[req.headers.origin].index++;
        }else{
            //不存在
            global.ipintercept[req.headers.origin]={index:1,}
            setTimeout(()=>{
                // console.log(global.ipintercept[req.headers.origin].index)
                if(global.ipintercept[req.headers.origin].index>50){
                    //达到频繁访问条件
                    global.interceptlibrary.push(req.headers.origin);
                    setTimeout(()=>{
                        //从拦截库中取消
                        global.interceptlibrary.splice(global.interceptlibrary.indexOf(req.headers.origin),1);
                    },600000);
                }
                delete global.ipintercept[req.headers.origin];
            },1000)
        }
    }
}

if(typeof module === "undefined") var module = {exports}

module.exports = frequen;